package mocks;

/**
 * For testing, and can show the general example of 
 * how to use...
 * @author psyomn
 *
 */

import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
import java.util.ArrayList;

import gms.View;
import helpers.SimpleSendMessageService;
import helpers.UDPListeningService;

public class MockBoxOffice implements Runnable
{

	/**
	 * This is the port which this BoxOffice listens at.
	 */
	private int mPort;

	/**
	 * This is to not hardcode literals...
	 */
	private int mTypicalGMSPort = 20000; 
	
	/**
	 * Name of the boxoffice
	 */
	private String mName;

	/**
	 * This is mainly here for debugging purposes. Setting it to true will print
	 * verbose output on the console.
	 */
	@SuppressWarnings("unused")
  private static final boolean mDebug = true;
	
	/**
	 * The hostname to connect to
	 */
	private String mHostnameToConnectTo;

	/**
	 * This is to send the message to the GMS (local or foreign)
	 */
	private SimpleSendMessageService mSSMS;
	
	/**
	 * Flag to set if leader or not. Is set as false by default
	 * upon object creation. 
	 */
	private boolean mIsLeader = false;

	/**
	 * Whether process terminated or not
	 */
	private boolean mTerminated = false;

	/**
	 * The socket we are listening from
	 */
	private DatagramSocket mSocketHandle;

	private String mMessage; 
	
	private String mHostnameToConnect; 
	
	private ArrayList<String> mViews; 
	
	/**
	 * Default constructor with parameters. Initializes the variables
	 * 
	 * @param name
	 * @param port
	 */
	public MockBoxOffice(String name, int port, String hostnametoconnect, ArrayList<String> views)
	{
		mViews = views; 
		mName = name;
		mPort = port;
		mHostnameToConnect = hostnametoconnect; 
		
		/* This should send a message to */
		mSSMS = new SimpleSendMessageService(hostnametoconnect, "bxf:" + mName, mTypicalGMSPort);
		
		try
    {
	    mSocketHandle = new DatagramSocket(port);
    } 
		catch (SocketException e)
    {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
    }
		
		System.out.println("Started Boxoffice [" 
				+ name + "] at port " + "[" 
				+ port
		    + hostnametoconnect + "]");
	}

	/**
	 * Main running procedure.
	 */
	@Override
	public void run()
	{
		Thread t = new Thread(mSSMS);

		/* This might need rewriting... */
		String mess = new String();

		byte[] receiveData = new byte[100];
		DatagramPacket p; 
		String tostr; 
		
		t.start();
		
	  while(!mTerminated)
	  {
	  	try
	  	{
	  		System.out.println("[BxOff:"+mPort+"] listening...");
	  		p = new DatagramPacket(receiveData, receiveData.length); 
	  		mSocketHandle.receive(p);
	  		tostr = new String(receiveData);

	  		mMessage = tostr; 
	  		whatdo(mMessage);
	  	}
	  	catch(Exception e)
	  	{
	  		e.getStackTrace();
	  		
	  		/*System.out.println(
	  				this.getClass().getName()
	  				+ ":"
	  				+ e.getMessage());*/
	  	}
	  }
	  
	  System.out.println("##Boxoffice dead");
	  
	}

	/**
	 * Basically the commands. This should been called
	 * command but I was really sleep deprived when I 
	 * tried doing this...
	 * @param message
	 */
	private void whatdo(String message)
  {
	  if (message.trim().equalsIgnoreCase("die"))
	  {
	  	mSSMS.terminate();
	  	mSocketHandle = null;
	  	mTerminated = true;
	  }
	  
  }

	/**
	 * @return the name of the boxoffice
	 */
	public String getName()
  {
	  return mName;
  }
	
	/** TODO
	 * This is required in order to spawn from the GMS
	 * @param args
	 * 	Should be in the following format: 
	 * 		NAME HOSTNAME PORT
	 */
	public static void main(String[] args)
	{
//		MockBoxOffice mbo; 
//		String name; 
//		String hostname; 
//		int port; 
//		 j
	}

}
